package com.sinothk.fc.bo.demand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.bo.demand.entity.BoDemandEntity;
import com.sinothk.fc.bo.demand.entity.BoDemandSearchReq;
import com.sinothk.fc.bo.demand.entity.BoDemandVO;
import com.sinothk.fc.bo.demand.mapper.BoDemandMapper;
import com.sinothk.fc.bo.demand.service.IBoDemandService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 商机需求信息
 * @Author: Liangyt
 * @Date: 2022-07-17
 * @Version: V1.0
 */
@Service
public class BoDemandServiceImpl extends ServiceImpl<BoDemandMapper, BoDemandEntity> implements IBoDemandService {

    @Resource
    private BoDemandMapper boBiddingMapper;

    @Override
    public BoDemandEntity addOrUpdate(BoDemandEntity boBiddingEntity) {
        this.saveOrUpdate(boBiddingEntity);
        return boBiddingEntity;
    }

    @Override
    public IPage<BoDemandEntity> listForMinePage(Page<BoDemandEntity> page, BoDemandEntity beanReq, HttpServletRequest req) {
        QueryWrapper<BoDemandEntity> queryWrapper = QueryGenerator.initQueryWrapper(beanReq, req.getParameterMap());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.lambda().eq(BoDemandEntity::getCreateBy, loginUser.getId());
        queryWrapper.lambda().orderByAsc(BoDemandEntity::getState);
        queryWrapper.lambda().orderByDesc(BoDemandEntity::getCreateTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<BoDemandVO> listForBo(Page<BoDemandEntity> page, BoDemandSearchReq searchReq) {
//        QueryWrapper<BoBiddingEntity> queryWrapper = QueryGenerator.initQueryWrapper(beanReq, req.getParameterMap());
//        queryWrapper.lambda().eq(BoBiddingEntity::getState, 1);// 状态:0草稿1招标中2已完成3已中止
//        queryWrapper.lambda().orderByDesc(BoBiddingEntity::getCreateTime);
//        return this.page(page, queryWrapper);

        return boBiddingMapper.listForBo(page, searchReq);
    }

    @Override
    public IPage<BoDemandVO> demandSupplyForMePage(Page<BoDemandEntity> page, BoDemandSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());
        return boBiddingMapper.demandSupplyForMePage(page, searchReq);
    }
}
